<template>
	<view class="evaluate_wrapper">		
		<u-alert type="primary" description="客观真实的评价能帮助用户做决策" :show-icon="true"></u-alert>
		<view class="product_info" v-if="Object.keys(productInfo).length > 0">			
			<i class="iconfont icon-dianpu"></i>
			<text class="name">{{productInfo.shopname}}</text>			
		</view>
		<view class="user_evalute">
			<label>评分</label>
			<u-rate active-color="#FA3534" inactive-color="#b2b2b2" v-model="userForm.rate" @change="evalValueChange"></u-rate>
		</view>
		<u--textarea v-model="userForm.message" placeholder="从多个角度评价宝贝，可以帮助更多想买的人" maxlength="50" count></u--textarea>
		<view class="footer_btn">
			<u-button type="warning" text="发布" @click="userRelease"></u-button>
		</view>
	</view>
</template>

<script>
	import { api_getOneColumn,api_getOneExamInfo,api_getOneKnowledgeInfo,api_getEvaluate } from '@/api/api'
	import { URLParams } from '@/utils/util'
	export default {	
		data() {
			return {
				productInfo: {},
				userForm: {
					rate: 2,
					message: ''
				},
				errorProduct: require('@/static/img/default_image.jpg'), 
			}
		},
		onLoad() {
			this.init();
		},
		methods: {
			init(){	// 页面初始化
				let pageQuery = URLParams(window.location.href);
				if (pageQuery.type == 'column') {
					api_getOneColumn({id: pageQuery.id}).then(res => {
						if (res.code == 1) {
							this.productInfo = res.data;
						} else {
							uni.showToast({ title: res.msg,icon: 'error' })
						}
					}).catch(err => console.log(err));
				} else if (pageQuery.type == 'exam') {
					api_getOneExamInfo({id: pageQuery.id}).then(res => {
						if (res.code == 1) {
							this.productInfo = res.data;
						} else {
							uni.showToast({ title: res.msg,icon: 'error' })
						}
					}).catch(err => console.log(err));
				} else {
					api_getOneKnowledgeInfo({id: pageQuery.id}).then(res => {
						if (res.code == 1) {
							this.productInfo = res.data;
						} else {
							uni.showToast({ title: res.msg,icon: 'error' })
						}
					}).catch(err => console.log(err));
				}
			},			
			evalValueChange(val){	// 星星评分
				this.userForm.rate = val;				
			},
			userRelease(){	// 发布
				api_getEvaluate(this.userForm).then(res => {
					if (res.code == 1) {
						uni.showToast({ title: '谢谢您，添加评价成功!',icon: 'error' })
					} 
					if (res.code == -2) {
						uni.showToast({ title: '该订单已经评论过了',icon: 'error' })
					}
					if (res.code !== 1 && res.code !== -2) {
						uni.showToast({ title: '对不起，添加评价失败!',icon: 'error' })
					}
				}).catch(err => console.log(err));
			}
		}
	}
</script>

<style lang="scss" scoped>
.evaluate_wrapper{
	.product_info{
		display: flex;
		align-items: center;
		padding: 10px;
		box-sizing: border-box;
		.iconfont{
			display: block;
			font-size: 24px;			
		}
		.name{
			display: block;
			font-size: 16px;
			padding: 0 0 0 5px;
		}
	}
	.user_evalute{
		display: flex;
		margin: 10px 15px;
		padding: 0 0 10px;
		border-bottom: 1px solid #eee;
	}
	::v-deep .u-textarea{
		margin: 0 10px 50px;
		border: none;
	}
	.footer_btn{
		padding: 0 10px;
		line-height: 40px;
		overflow: hidden;
	}
}
</style>
